<?php
namespace app\manager\controller;
use think\Controller;
use think\Request;
use Nette\Mail\Message;
use Nette\Mail\SendmailMailer;
class CampManagerList extends BaseController
{
    const ADMIN_MANAGER = 0;//超级管理员
    const GENERAL_MANAGER = 1;//一般管理员(未锁定)
    const RESTRICTED_MANAGER = -99;//锁定管理员

    protected $beforeActionList = [
        'first',
        'checkLogin' => ['except' => 'getlogin,getlogout,getforgetpassword,postsendpwd,postnewpwd,postlogin,'],
        'checkPower' => ['except' => 'index,resetPwd,postResetPwd,managerInfo,saveManagerInfo']
    ];

    public function checkPower()
    {
        $id = session('manager.id');
        $count = model('CampManagers')->checkManagerPower($id);
        if ($count) {
            return true;
        }
        view('p/auth')->send();
        exit;
    }

    /**
     * 
     * @return [type] [description]
     */
    public function index()
    {
        //halt(session('manager'));
        $id = session('manager.id');
        $camp_id = model('CampManagers')->where('id',$id)->value('camp_id');
        $list = model('CampManagers')->getCampManagerLists($camp_id);//halt($list);
        $this->assign('list',$list);
        $id = session('manager.id');
        $this->assign('id',$id);
        return view('p/campmanagerlist/index');
    }

    /**
     * GET show camptype info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function read($id)
    {
        $data = model('CampManagers')->getCampManagerInfo($id);//halt($data->status);
        $this->assign('data',$data);
        $this->assign('id',$id);
        return view('p/campmanagerlist/read');
    }

    /**
     * GET shwo create campmanagerlist info
     * @return [type] [description]
     */
    public function create()
    {
        $id = session('manager.id');
        $this->assign('id',$id);
        return view('p/campmanagerlist/create');
    }

    /**
     * POST create new camptype info
     * @return [type] [description]
     */
    public function save()
    {
        $id = session('manager.id');
        $campmanager_info = model('CampManagers')->where('id',$id)->find();
        //$post_campmanager_info['camp_id'] = model('CampManagers')->where('id',$id)->value('camp_id');
        $post_campmanager_info['camp_id'] = $campmanager_info->camp_id;
        $post_campmanager_info['branch_id'] = $campmanager_info->branch_id;
        $post_campmanager_info['status'] = 1;//1为普通管理员
        $post_campmanager_info['name'] = input('post.name');
        $post_campmanager_info['phone'] = input('post.phone');
        $post_campmanager_info['password'] = md5(input('post.password'));
        $post_campmanager_info['email'] = input('post.email');
        if (empty(input('post.name') && input('post.phone') && input('post.password') && input('post.email'))) {
            return $this->error('请填写完整信息');
        }

        $campmanager = model('CampManagers')->createCampManager($post_campmanager_info);        

        if($campmanager){
            return $this->success('创建成功','/campmanagerlist');
        }
            return $this->error('创建失败');
    }

    /**
     * GET edit camptype info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function edit($id)
    {
        $data = model('CampManagers')->getCampManagerInfo($id);
        $this->assign('data',$data);
        $this->assign('id',$id);
        return view('p/campmanagerlist/edit');
    }

    /**
     * PUT update camptype info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function update($id)
    {
        $post_campmanager_info['name'] = input('post.name');
        $post_campmanager_info['phone'] = input('post.phone');
        $post_campmanager_info['email'] = input('post.email');
        $post_campmanager_info['idnumber'] = input('post.idnumber');
        $post_campmanager_info['remark'] = input('post.remark');
        $campmanager = model('CampManagers')->updateCampManagerInfo($id, $post_campmanager_info);

        if($campmanager){
            //return json($this->ajaxReturn(1,lang('update camptype success')));
            return $this->success('更新成功','/campmanagerlist');
        }
            //return json($this->ajaxReturn(0,lang('update camptype failed')));
            return $this->error('更新失败');
    }

    /**
     * DELETE camptype info
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete()
    {
        $ids=input('post.ids/a');
        if (empty($ids)) {
          return $this->error('请选择要删除的选项'); 
        }
        $count = model('CampManagers')->delInfo($ids);
        if ($count) {
           return $this->success('删除成功','/campmanagerlist');
        }
        return $this->error('删除失败');
    }

    public function luck()
    {
        $id = input('post.id');
        $status = self::RESTRICTED_MANAGER;
        $count = model('CampManagers')->setLuck($id,$status);
        if ($count) {
            return $this->success('锁定成功','/campmanagerlist');
        }
        return $this->error('锁定失败');
    }

    public function unluck()
    {
        $id = input('post.id');
        $status = self::GENERAL_MANAGER;
        $count = model('CampManagers')->setUnluck($id,$status);
        if ($count) {
            return $this->success('解除锁定成功','/campmanagerlist');
        }
        return $this->error('解除锁定失败');
    }
    
}

